﻿using System;
using AccessControl.Wpf.ViewModels;
using AccessControl.Contracts.Managers;
using System.Windows.Input;
using AccessControl.Wpf.Commands;
using AccessControl.Contracts.Models;
using AccessControl.Gateway.Managers;

namespace AccessControl.Wpf.Events.ViewModels
{
	class EventsViewModel : ViewModel
	{
		#region Fields

		IEventsManager _EventsManager;

		#endregion

		#region Public constructor

		public EventsViewModel(IEventsManager eventsManager)
		{
			EventsManager = eventsManager;
		}

		#endregion

		#region Public Properties

		public IEventsManager EventsManager
		{
			get { return _EventsManager; }
			private set
			{
				if (_EventsManager != value)
				{
					_EventsManager = value;
					NotifyPropertyChanged("EventsManager");
				}
			}
		}

		#endregion

		#region Commands

		public ICommand UpdateEventCommand
		{
			get { return new CommandBaseOfT<IEvent>(UpdateEvent); }
		}

		#endregion

		private void UpdateEvent(IEvent myEvent)
		{
			if (myEvent.Handled)
				myEvent.HandlingOperator = Session.Instance.LoggedInOperator.UserName;
			else
				myEvent.HandlingOperator = string.Empty;

			EventsManager.Update(myEvent);
		}
	}
}
